package cn.lysj.framework.pirpraph.service;

import cn.lysj.framework.common.utils.random.CustomTimeType;
import cn.lysj.framework.common.utils.random.CustomTimeUtil;
import cn.lysj.framework.common.utils.random.WeekChoice;
import cn.lysj.framework.common.utils.web.Resp;
import cn.lysj.framework.order.service.FoodOrderService;
import cn.lysj.framework.pirpraph.vo.PirPraphVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author ZhangWenJian
 * @data 2018/12/29--15:48
 * @description
 */
@Slf4j
@Service
@Transactional
public class PirPraphService {

    @Resource
    private FoodOrderService foodOrderService;

    public Resp getPirPraphVo(String storId,Integer rank,Integer number){
        Integer skewingNumber = number - 1;
        Integer arrayBadge = 0;
        List<Object[]> objects = getStatObjects(rank,skewingNumber,storId);
        String[] timeData = new String[number]; //时间数组
        BigDecimal moneyMax =  BigDecimal.ZERO;//钱最大值
        BigInteger salesMax = BigInteger.ZERO; //销量最大值
        BigDecimal[] moneyData = new BigDecimal[number]; //钱数组数据
        BigInteger[] salesData = new BigInteger[number];//销量数组数据
        for (int i = 0;i<objects.size();i++){
            Object object[] = objects.get(i);
        }
//        循环将查询到的数据插入数组
        for (int i = 0; i<objects.size() ;i++){
//        for (int i = objects.size()-1; i>=0 ;i--){
            Object object[] = objects.get(i);
            String date = (String) object[0];
            String swfDate = getSwfDate(rank,skewingNumber);
            BigDecimal money = (BigDecimal) object[1];
            BigInteger sales = (BigInteger) object[2];
            //最大值判断
            if(moneyMax.compareTo(money) < 0){
                moneyMax = money;
            }
            if(salesMax.compareTo(sales) < 0){
                salesMax = sales;
            }
            while (!date.equals(swfDate)){
//                System.out.println(date+":"+swfDate);
                timeData[arrayBadge] = swfDate;
                moneyData[arrayBadge] = BigDecimal.ZERO;
                salesData[arrayBadge] = BigInteger.ZERO;
                arrayBadge += 1;
                skewingNumber -= 1;
                swfDate = getSwfDate(rank,skewingNumber);
            }
            timeData[arrayBadge] = date;
            moneyData[arrayBadge] = money;
            salesData[arrayBadge] = sales;
            arrayBadge += 1;
            skewingNumber -= 1;
        }
        //如果数组未填满
        while (arrayBadge <= number-1){
            timeData[arrayBadge] = getSwfDate(rank,skewingNumber);
            moneyData[arrayBadge] = BigDecimal.ZERO;
            salesData[arrayBadge] = BigInteger.ZERO;

            arrayBadge += 1;
            skewingNumber -= 1;
        }
        moneyMax = moneyMax.add(moneyMax.multiply(new BigDecimal(0.06))).setScale(0,BigDecimal.ROUND_UP);
        salesMax = salesMax.add(salesMax.divide(BigInteger.valueOf(10))).add(BigInteger.ONE);
        return new Resp(new PirPraphVo(timeDataChange(timeData,rank), moneyMax, salesMax, moneyData, salesData));
//        return new Resp(objects);
    }

    public String[] timeDataChange(String[] timeData,Integer rank){
        String [] timeDataChange = timeData;
        switch (rank){
            case 1:
                break;
            case 2:
                timeDataChange = weekTimeDataChange(timeDataChange);
                break;
            case 3:
                break;
            case 4:
                break;
            case 5:
                break;
            default:
                break;

        }
        return timeDataChange;

    }

    public String[] weekTimeDataChange(String[] timeData){
        String[] returnTimeData = new String[timeData.length];
        for (int i = 0;i<timeData.length;i++){
            String resultString  = "";
            String[] timeDataSqlit = timeData[i].split("-");
            Integer year = Integer.valueOf(timeDataSqlit[0]);
            Integer week = Integer.valueOf(timeDataSqlit[1]);
            resultString = weekTimeDataChangeTool(year,week,Calendar.MONDAY) +"~"+ weekTimeDataChangeTool(year,week,Calendar.SUNDAY);
            returnTimeData[i] = resultString;
        }
        return returnTimeData;
    }

    public String weekTimeDataChangeTool(Integer year,Integer week,Integer dayOfWeek){
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.set(Calendar.YEAR,year);
        calendar.set(Calendar.WEEK_OF_YEAR,week);
        calendar.set(Calendar.DAY_OF_WEEK,dayOfWeek);
        return calendar.get(Calendar.YEAR)+"."+(calendar.get(Calendar.MONTH)+1)+"."+calendar.get(Calendar.DATE);
    }
    /**
     * @author ZhangWenJian
     * @date 2019/1/2 13:32
     * @title getWeekData
     * @param rank 等级1.天 2.周 3.月 4.季节 5.年
     * @param skewingNumber 偏移量
     * @return java.lang.String
     * @description
     */
    public String getSwfDate(Integer rank,Integer skewingNumber){
        String date = "";
        switch (rank){
            case 1:
                date = getSkewingTimeOfShowYearAndDayAndHour(-skewingNumber);
                break;
            case 2:
                date = getSkewingTimeOfShowYearAndWeek(-skewingNumber);
                break;
            case 3:
                date = getSkewingTimeOfShowYearAndMonth(-skewingNumber);
                break;
            case 4:
                break;
            case 5:
                date = getSkewingTimeOfShowYear(-skewingNumber);
                break;
            default:
                break;
        }
        return date;
    }

    /**
     * @author ZhangWenJian
     * @date 2019/1/2 13:27
     * @title getStatObjects
     * @param rank 等级1.天 2.周 3.月 4.季节 5.年
     * @param skewingNumber 偏移量
     * @param storId 商户ID
     * @return java.util.List<java.lang.Object[]>
     * @description 差数据库数据
     */
    public List<Object[]> getStatObjects(Integer rank,Integer skewingNumber,String storId){
        Date date = new Date();
        List<Object[]> objects = null;
        switch (rank){
            case 1:
                objects =  foodOrderService.getRepository().findByDay(
                        CustomTimeUtil.getSkewingTimeInDay(date,-skewingNumber,CustomTimeType.BEGIN),
                        CustomTimeUtil.getSkewingTimeInDay(date,0,CustomTimeType.END),
                        storId);
                break;
            case 2:
                objects =  foodOrderService.getRepository().findByWeek(
                        CustomTimeUtil.getSkewingTimeInWeek(date,-skewingNumber,WeekChoice.MONDAY,CustomTimeType.BEGIN),
                        CustomTimeUtil.getSkewingTimeInWeek(date,0,WeekChoice.SUNDAY,CustomTimeType.END),
                        storId);
                objects = changeObjectsInWeek(objects);
                break;
            case 3:
                objects =  foodOrderService.getRepository().findByMonth(
                        CustomTimeUtil.getSkewingTimeInMonth(date, -skewingNumber,CustomTimeType.BEGIN),
                        CustomTimeUtil.getSkewingTimeInMonth(date, 0,CustomTimeType.END),
                        storId);
                break;
            case 4:
                objects =  foodOrderService.getRepository().findByQu(
                        CustomTimeUtil.getSkewingTimeInWeek(date,-skewingNumber,WeekChoice.MONDAY,CustomTimeType.BEGIN),
                        CustomTimeUtil.getSkewingTimeInWeek(date,0,WeekChoice.SUNDAY,CustomTimeType.END),
                        storId);
                break;
            case 5:
                objects =  foodOrderService.getRepository().findByYear(
                        CustomTimeUtil.getSkewingTimeInYear(date, -skewingNumber, CustomTimeType.BEGIN),
                        CustomTimeUtil.getSkewingTimeInYear(date, 0, CustomTimeType.END),
                        storId);
                break;
            default:
                break;
        }
        return objects;
    }

    /**
     * @author ZhangWenJian
     * @date 2019/1/4 9:08
     * @title changeObjectsInWeek
     * @param objects 传入数据库查询到的日期格式
     * @return java.util.List<java.lang.Object[]>
     * @description 用来修改数据库来的数据与工具类的区间不对称。
     */
    public List<Object[]> changeObjectsInWeek(List<Object[]> objects){
        List<Object[]> newObjects = new ArrayList<>();
        BigDecimal money = BigDecimal.ZERO;
        BigInteger sales = BigInteger.ZERO;
        boolean swift = false;
        for (int i = 0; i < objects.size() ;i++) {
            Object object[] = objects.get(i);
            String date = (String) object[0];
            String[] sqitDate = date.split("-");
            //此步检测是否为第53周,53周将会与工具类周不匹配,其将会划分至下一年的第一周
            if (sqitDate[1].equals("53") && i != objects.size() - 1){
                if(i == objects.size() -1){
                    object[0] = (Integer.valueOf(sqitDate[0])+1)+"-01";
                }
                else{
                    money = (BigDecimal) object[1];
                    sales = (BigInteger) object[2];
                    swift = true;
                    continue;
                }
            }
            if(swift && sqitDate[1].equals("01")){
                object[1] = new BigDecimal(String.valueOf(object[1])).add(money);
                object[2] = new BigInteger(String.valueOf(object[2])).add(sales);
                swift = false;
            }
            newObjects.add(object);
        }
        return newObjects;
    }

    /**
     * @author ZhangWenJian
     * @date 2019/1/2 16:22
     * @title getSkewingTimeOfShowYearAndMonth
     * @param monthSkewingNumber 偏移数量
     * @return java.lang.String
     * @description 获取月偏移后的字符串
     */
    public String getSkewingTimeOfShowYearAndMonth(Integer monthSkewingNumber){
        Date date = CustomTimeUtil.getSkewingTimeInMonth(new Date(), monthSkewingNumber,CustomTimeType.BEGIN);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int monthInt = calendar.get(Calendar.MONTH)+1;
        String month = "";
        if((monthInt)/10 < 1){
            month = "0"+ monthInt;
        }
        else {
            month = "" + monthInt;
        }
        return calendar.get(Calendar.YEAR)+"-"+month;
    }
    /**
     * @author ZhangWenJian
     * @date 2019/1/2 10:22
     * @title getSkewingTimeOfShowYearAddWeek
     * @param weekSkewingNumber 偏移数量
     * @return java.lang.String
     * @description 获取偏移周的integet值
     */
    public String getSkewingTimeOfShowYearAndWeek(Integer weekSkewingNumber){
        Date date = CustomTimeUtil.getSkewingTimeInWeek(new Date(),weekSkewingNumber, WeekChoice.SUNDAY, CustomTimeType.CONSTANT);
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.setTime(date);
        int weekInt = calendar.get(Calendar.WEEK_OF_YEAR);
        String week = "";
        if((weekInt)/10 < 1){
            week = "0"+ weekInt;
        }
        else {
            week = "" + weekInt;
        }
        return calendar.get(Calendar.YEAR)+"-"+week;
    }

    /**
     * @author ZhangWenJian
     * @date 2019/1/2 14:20
     * @title getSkewingTimeOfShowYearAndDayAndHour
     * @param daySkewingNumber 偏移日
     * @return java.lang.String
     * @description
     */
    public static String getSkewingTimeOfShowYearAndDayAndHour(Integer daySkewingNumber){
        Date date = CustomTimeUtil.getSkewingTimeInDay(new Date(),daySkewingNumber, CustomTimeType.BEGIN);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr = sdf.format(date);
        return dateStr;
    }
    /**
     * @author ZhangWenJian
     * @date 2019/1/2 16:22
     * @title getSkewingTimeOfShowYear
     * @param yearSkewingNumber 偏移年
     * @return java.lang.String
     * @description 年偏移
     */
    public static String getSkewingTimeOfShowYear(Integer yearSkewingNumber){
        Date date = CustomTimeUtil.getSkewingTimeInYear(new Date(),yearSkewingNumber, CustomTimeType.BEGIN);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.YEAR)+"";
    }





}
